//
//  VisitorLoginView.swift
//  weibosina
//
//  Created by 李明禄 on 15/10/9.
//  Copyright © 2015年 李明禄. All rights reserved.
//

import UIKit

// MARK: 定义访客视图协议
protocol VisitorLoginViewDelegate: NSObjectProtocol {
    //用户将要登录
    func visitorLoginViewWillLogin()
    func visitorLoginViewWillRegister()
    
    //用户将要注册
}


class VisitorLoginView: UIView {
    
    //定义代理属性
    weak var delegate: VisitorLoginViewDelegate?
    
    //按钮监听方法
    func clickLogin() {
        delegate? .visitorLoginViewWillLogin()
    }
    
    func clickLogout() {
        delegate? .visitorLoginViewWillRegister()
    }
    
    
    
    func setupViewInfo(isHome: Bool, imageName: String, message: String) {
    
        //如果是首页就开始动画,否则就将遮罩隐藏
        isHome ? startAnimation() : sendSubviewToBack(maskIconView)
        iconView.image = UIImage(named: imageName)
        infoLabel.text = message
        houseIconView.hidden = !isHome
    
    }


    ///开始动画
    private func startAnimation() {
        /* The key-path describing the property to be animated. */
        let anim = CABasicAnimation(keyPath: "transform.rotation")
//        anim.toValue = 旋转弧度
        anim.toValue = 2 * M_PI
        anim.repeatCount = MAXFLOAT
        anim.duration = 20.0
        houseIconView.layer.addAnimation(anim, forKey: nil)
        
        anim.removedOnCompletion = false
    }
    
    
    // MARK: -  界面初始化
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        setupUI()
    }

    required init?(coder aDecoder: NSCoder) {
        
        //将下面的代码进行注释,否则不会执行setupUI()
//        fatalError("init(coder:) has not been implemented")
        super.init(coder: aDecoder)
        setupUI()
    }
    
    private func setupUI() {
        
        //1.添加控件
        addSubview(iconView)
        addSubview(maskIconView)
        addSubview(houseIconView)
        addSubview(infoLabel)
        addSubview(loginButton)
        addSubview(registerButton)
        
        startAnimation()
        
        //2.设置自动布局 
        
        /**
            布局原则:
        所有的控制都参考一个控件,这样做的目的是当更改位置的时候就会全动,否则修改的地方比较多
        修改比较麻烦
        */
        
        //每次设置时需要将autoresizing设置为false
        /**
        /* By default, the autoresizing mask on a view gives rise to constraints that fully determine
        the view's position. This allows the auto layout system to track the frames of views whose
        layout is controlled manually (through -setFrame:, for example).
        When you elect to position the view using auto layout by adding your own constraints,
        you must set this property to NO. IB will do this for you.
        */
        
        /**
        +(instancetype)
        constraintWithItem:(id)  view1
        attribute:(NSLayoutAttribute)  attr1
        relatedBy:(NSLayoutRelation)  relation
        toItem:(id)  view2
        attribute:(NSLayoutAttribute) attr2
        multiplier:(CGFloat)  multiplier
        constant:(CGFloat)  c;
        
        该方法实际上就是满足一个数学关系
        
        item1 =(>=,<=) multiplier * item2 + constant。
        
        参数说明：
        
        view1：第一个视图即item1。
        
        attr1：是第一个视图选择的属性
        
        relation：即中间的关系（= , >= , <=）
        
        view2：第二个视图即item2。
        
        attr2：是第二个视图选择的属性
        
        NSLayoutConstraint(item: <#T##AnyObject#>, attribute: <#T##NSLayoutAttribute#>, relatedBy: <#T##NSLayoutRelation#>, toItem: <#T##AnyObject?#>, attribute: <#T##NSLayoutAttribute#>, multiplier: <#T##CGFloat#>, constant: <#T##CGFloat#>)
        */
        
        */
        //<1 图标
        
        iconView.translatesAutoresizingMaskIntoConstraints = false
        addConstraint(NSLayoutConstraint(item: iconView, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.CenterX, multiplier: 1.0, constant: 0))
        
        addConstraint(NSLayoutConstraint(item: iconView, attribute: NSLayoutAttribute.CenterY, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.CenterY, multiplier: 1.0, constant: -10))
        
        //<2遮罩
//        maskIconView.translatesAutoresizingMaskIntoConstraints = false
//        addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("H:|-0-[subview]-0-|", options: NSLayoutFormatOptions(rawValue: 0), metrics: nil, views: ["subview" : maskIconView]))
//        addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:|-0-[subview]-(-27)-[regButton]", options: NSLayoutFormatOptions(rawValue: 0), metrics: nil, views: ["subview": maskIconView, "regButton": registerButton]))
        
        //<3房子
        houseIconView.translatesAutoresizingMaskIntoConstraints = false
        addConstraint(NSLayoutConstraint(item: houseIconView, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: iconView, attribute: NSLayoutAttribute.CenterX, multiplier: 1.0, constant: 0))
        addConstraint(NSLayoutConstraint(item: houseIconView, attribute: NSLayoutAttribute.CenterY, relatedBy: NSLayoutRelation.Equal, toItem: iconView, attribute: NSLayoutAttribute.CenterY, multiplier: 1.0, constant: 0))
        
        //<4 消息
        //相对于iconView
        infoLabel.translatesAutoresizingMaskIntoConstraints = false
        addConstraint(NSLayoutConstraint(item: infoLabel, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: iconView, attribute: NSLayoutAttribute.Bottom, multiplier: 1.0, constant: 20))
        addConstraint(NSLayoutConstraint(item: infoLabel, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: iconView, attribute: NSLayoutAttribute.CenterX, multiplier: 1.0, constant: 0))
        //设置消息的宽度,为的是显示文字信息需要换行,否则显示在一行上
        addConstraint(NSLayoutConstraint(item: infoLabel, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1.0, constant: 200))
       
        
        //<5登录按钮
        loginButton.translatesAutoresizingMaskIntoConstraints = false
        addConstraint(NSLayoutConstraint(item: loginButton, attribute: NSLayoutAttribute.Left, relatedBy: NSLayoutRelation.Equal, toItem: infoLabel, attribute: NSLayoutAttribute.Left, multiplier: 1.0, constant: 0))
        addConstraint(NSLayoutConstraint(item: loginButton, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: infoLabel, attribute: NSLayoutAttribute.Bottom, multiplier: 1.0, constant: 20))
        addConstraint(NSLayoutConstraint(item: loginButton, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1.0, constant: 60))
        addConstraint(NSLayoutConstraint(item: loginButton, attribute: NSLayoutAttribute.Height, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1.0, constant: 30))
        
        
        
        
        //<6注册按钮
        
        registerButton.translatesAutoresizingMaskIntoConstraints = false
        addConstraint(NSLayoutConstraint(item: registerButton, attribute: NSLayoutAttribute.Right, relatedBy: NSLayoutRelation.Equal, toItem: infoLabel, attribute: NSLayoutAttribute.Right, multiplier: 1.0, constant: 0))
        addConstraint(NSLayoutConstraint(item: registerButton, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: infoLabel, attribute: NSLayoutAttribute.Bottom, multiplier: 1.0, constant: 20))
        addConstraint(NSLayoutConstraint(item: registerButton, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1.0, constant: 60))
        addConstraint(NSLayoutConstraint(item: registerButton, attribute: NSLayoutAttribute.Height, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1.0, constant: 30))
        
        
        
        maskIconView.translatesAutoresizingMaskIntoConstraints = false
        addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("H:|-0-[subview]-0-|", options: NSLayoutFormatOptions(rawValue: 0), metrics: nil, views: ["subview" : maskIconView]))
        addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:|-0-[subview]-(-35)-[regButton]", options: NSLayoutFormatOptions(rawValue: 0), metrics: nil, views: ["subview": maskIconView, "regButton": registerButton]))
        //<7设置背景颜色
        backgroundColor = UIColor(white: 237.0 / 255.0, alpha: 1.0)


        
    
    }
    
    
  // MARK: - 懒加载界面控件
    //懒加载转场图像
    private lazy var iconView: UIImageView = {
        let iv = UIImageView(image: UIImage(named: "visitordiscover_feed_image_house"))
        return iv
        
        }()
    
    //懒加载遮罩
    private lazy var maskIconView: UIImageView = {
        let iv = UIImageView(image: UIImage(named: "visitordiscover_feed_mask_smallicon"))
        return iv
        
        }()
    
    
    //懒加载房子
    private lazy var houseIconView: UIImageView = {
        let hv = UIImageView(image: UIImage(named: "visitordiscover_feed_image_smallicon"))
        return hv
        
        }()
    
    
    
    
    //懒加载信息
    private lazy var infoLabel: UILabel = {
        
        let label = UILabel()
        label.text = "说点什么sssss"
        
        label.numberOfLines = 0
        label.font = UIFont.systemFontOfSize(12)
        label.textAlignment = NSTextAlignment.Center
        
        label.sizeToFit()
        
        return label
        }()
    
    
    //懒加载注册按钮
    private lazy var registerButton: UIButton = {
        let button = UIButton()
        button.setTitle("注册", forState: UIControlState.Normal)
        button.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: UIControlState.Normal)
        button.setTitleColor(UIColor.darkGrayColor(), forState: UIControlState.Normal)
        return button
    }()
    
    
    //懒加载登录按钮
    
    private lazy var loginButton: UIButton = {
        let button = UIButton()
        button.setTitle("登录", forState: UIControlState.Normal)
        button.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: UIControlState.Normal)
        button.setTitleColor(UIColor.darkGrayColor(), forState: UIControlState.Normal)
        
        return button
        }()


}
